package com.example.chamberlainserver.controller;

import com.example.chamberlainserver.Entry.StorageLocation;
import com.example.chamberlainserver.Service.StorageLocationService;
import com.example.chamberlainserver.Vo.Response.ApiResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 存放位置管理控制器
 */
@RestController
@RequestMapping("/location")
public class StorageLocationController {

    @Autowired
    private StorageLocationService storageLocationService;

    /**
     * 添加存放位置
     * @param location 存放位置信息
     * @return 添加结果
     */
    @PostMapping("/add")
    public ResponseEntity<ApiResponse<StorageLocation>> addLocation(@RequestBody StorageLocation location) {
        try {
            StorageLocation savedLocation = storageLocationService.addLocation(location);
            return ResponseEntity.ok(ApiResponse.success("添加存放位置成功", savedLocation));
        } catch (Exception e) {
            return ResponseEntity.ok(ApiResponse.error("添加存放位置失败：" + e.getMessage()));
        }
    }

    /**
     * 更新存放位置信息
     * @param location 存放位置信息
     * @return 更新结果
     */
    @PutMapping("/update")
    public ResponseEntity<ApiResponse<StorageLocation>> updateLocation(@RequestBody StorageLocation location) {
        try {
            StorageLocation updatedLocation = storageLocationService.updateLocation(location);
            return ResponseEntity.ok(ApiResponse.success("更新存放位置成功", updatedLocation));
        } catch (Exception e) {
            return ResponseEntity.ok(ApiResponse.error("更新存放位置失败：" + e.getMessage()));
        }
    }

    /**
     * 删除存放位置
     * @param id 存放位置ID
     * @return 删除结果
     */
    @DeleteMapping("/delete/{id}")
    public ResponseEntity<ApiResponse<Void>> deleteLocation(@PathVariable Integer id) {
        try {
            boolean success = storageLocationService.deleteLocation(id);
            if (success) {
                return ResponseEntity.ok(ApiResponse.success("删除存放位置成功", null));
            } else {
                return ResponseEntity.ok(ApiResponse.error("删除存放位置失败：位置不存在或已被删除"));
            }
        } catch (Exception e) {
            return ResponseEntity.ok(ApiResponse.error("删除存放位置失败：" + e.getMessage()));
        }
    }

    /**
     * 根据ID获取存放位置
     * @param id 存放位置ID
     * @return 存放位置信息
     */
    @GetMapping("/get/{id}")
    public ResponseEntity<ApiResponse<StorageLocation>> getLocationById(@PathVariable Integer id) {
        try {
            StorageLocation location = storageLocationService.getLocationById(id);
            if (location != null) {
                return ResponseEntity.ok(ApiResponse.success("获取存放位置成功", location));
            } else {
                return ResponseEntity.ok(ApiResponse.error("获取存放位置失败：位置不存在"));
            }
        } catch (Exception e) {
            return ResponseEntity.ok(ApiResponse.error("获取存放位置失败：" + e.getMessage()));
        }
    }

    /**
     * 获取所有存放位置
     * @return 存放位置列表
     */
    @GetMapping("/list")
    public ResponseEntity<ApiResponse<List<StorageLocation>>> getAllLocations() {
        try {
            List<StorageLocation> locations = storageLocationService.getAllLocations();
            return ResponseEntity.ok(ApiResponse.success("获取存放位置列表成功", locations));
        } catch (Exception e) {
            return ResponseEntity.ok(ApiResponse.error("获取存放位置列表失败：" + e.getMessage()));
        }
    }

    /**
     * 获取存放位置树结构
     * @return 存放位置树结构
     */
    @GetMapping("/tree")
    public ResponseEntity<ApiResponse<List<StorageLocation>>> getLocationTree() {
        try {
            List<StorageLocation> locationTree = storageLocationService.getLocationTree();
            return ResponseEntity.ok(ApiResponse.success("获取存放位置树结构成功", locationTree));
        } catch (Exception e) {
            return ResponseEntity.ok(ApiResponse.error("获取存放位置树结构失败：" + e.getMessage()));
        }
    }

    /**
     * 根据父ID获取子存放位置
     * @param parentId 父存放位置ID
     * @return 子存放位置列表
     */
    @GetMapping("/sub/{parentId}")
    public ResponseEntity<ApiResponse<List<StorageLocation>>> getSubLocations(@PathVariable Integer parentId) {
        try {
            List<StorageLocation> subLocations = storageLocationService.getSubLocations(parentId);
            return ResponseEntity.ok(ApiResponse.success("获取子存放位置成功", subLocations));
        } catch (Exception e) {
            return ResponseEntity.ok(ApiResponse.error("获取子存放位置失败：" + e.getMessage()));
        }
    }

    /**
     * 获取顶级存放位置
     * @return 顶级存放位置列表
     */
    @GetMapping("/top")
    public ResponseEntity<ApiResponse<List<StorageLocation>>> getTopLocations() {
        try {
            List<StorageLocation> topLocations = storageLocationService.getTopLocations();
            return ResponseEntity.ok(ApiResponse.success("获取顶级存放位置成功", topLocations));
        } catch (Exception e) {
            return ResponseEntity.ok(ApiResponse.error("获取顶级存放位置失败：" + e.getMessage()));
        }
    }

    /**
     * 根据位置名称搜索存放位置
     * @param name 位置名称关键字
     * @return 存放位置列表
     */
    @GetMapping("/search")
    public ResponseEntity<ApiResponse<List<StorageLocation>>> searchLocations(@RequestParam String name) {
        try {
            List<StorageLocation> locations = storageLocationService.searchLocationsByName(name);
            return ResponseEntity.ok(ApiResponse.success("搜索存放位置成功", locations));
        } catch (Exception e) {
            return ResponseEntity.ok(ApiResponse.error("搜索存放位置失败：" + e.getMessage()));
        }
    }

    /**
     * 根据管理员搜索存放位置
     * @param manager 管理员名称
     * @return 存放位置列表
     */
    @GetMapping("/manager")
    public ResponseEntity<ApiResponse<List<StorageLocation>>> getLocationsByManager(@RequestParam String manager) {
        try {
            List<StorageLocation> locations = storageLocationService.getLocationsByManager(manager);
            return ResponseEntity.ok(ApiResponse.success("获取管理员负责位置成功", locations));
        } catch (Exception e) {
            return ResponseEntity.ok(ApiResponse.error("获取管理员负责位置失败：" + e.getMessage()));
        }
    }

    /**
     * 更新存放位置路径
     * @param id 存放位置ID
     * @param locationPath 新的位置路径
     * @return 更新结果
     */
    @PutMapping("/update-path/{id}")
    public ResponseEntity<ApiResponse<StorageLocation>> updateLocationPath(
            @PathVariable Integer id, 
            @RequestParam String locationPath) {
        try {
            StorageLocation location = storageLocationService.updateLocationPath(id, locationPath);
            return ResponseEntity.ok(ApiResponse.success("更新位置路径成功", location));
        } catch (Exception e) {
            return ResponseEntity.ok(ApiResponse.error("更新位置路径失败：" + e.getMessage()));
        }
    }
}